﻿///////////////////////////////////////////////////////////////////////////////
// Copyright 2016 lingzimaike@gmail.com
// GPLv2
// Author: lingzimaike
///////////////////////////////////////////////////////////////////////////////

#ifndef PAPA_NET_IO_RUNNABLE_H_
#define PAPA_NET_IO_RUNNABLE_H_

#include <cstdint>
#include <event2/event.h>
#include <boost/atomic/atomic.hpp>
#include <papa/base.h>
#include <papa/thread/thread_runnable.h>

namespace papa {
namespace net {

	class TcpServer;

	class IORunnable : public thread::ThreadRunnable
	{
	public:
		IORunnable(void);
		virtual ~IORunnable(void);

		bool Open(void);
		void Stop(void);

		void IncrementLoad(void) { ++load_; }
		void DecrementLoad(void) { --load_; }
		::std::uint32_t get_load(void) const { return load_; }

		virtual void Run(void);

	private:
		friend class TcpServer;

		void Close(void);
		static void OnDummy(evutil_socket_t sock, short event, void *argument);

		struct ::event_base* event_base_;
		struct ::event* dummy_event_;
		::boost::atomic_uint32_t load_;
	};

}}  // namespace papa::net

#endif // PAPA_NET_IO_RUNNABLE_H_
